//
//  RecForViewController.m
//  A段项目
//
//  Created by dlios on 15/7/13.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "RecForViewController.h"
#import "BaseHeader.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface RecForViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollView;

@end

@implementation RecForViewController

- (void)dealloc
{
    [_array release];
    [_titleName release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
    }
    return  self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor blackColor];
    UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    titleView.text = [NSString stringWithFormat:@"1 / %lu", (unsigned long)self.array.count];
    titleView.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleView;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
    
    [self createScrollView];
    // Do any additional setup after loading the view.
}

- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 60, ScreenWidth - 20, ScreenHeidth - 70)];
    _scrollView.delegate = self;
    NSUInteger n = self.array.count;
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * n, 0);
    [self.view addSubview:self.scrollView];
    [self.scrollView release];
    for (int i = 1; i  < _array.count + 1; i++) {
        UIScrollView *scrollSmall = [[UIScrollView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width *(i - 1), 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height - 250)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, scrollSmall.frame.size.width, scrollSmall.frame.size.height)];
        imageView.tag = i;
        imageView.userInteractionEnabled = YES;
        NSDictionary *dic = [_array objectAtIndex:(i - 1)];
        [imageView sd_setImageWithURL:[dic objectForKey:@"imageUrl"] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        [tap release];
        
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, 20)];
        title.textColor = [UIColor whiteColor];
        title.text = self.titleName;
        
        NSString *str = [dic objectForKey:@"content"];
         NSDictionary *dic1 = @{NSFontAttributeName:[UIFont systemFontOfSize:14]};
        CGRect rect = [str boundingRectWithSize:CGSizeMake(ScreenWidth, 1000000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];
        UILabel *des = [[UILabel alloc] initWithFrame:CGRectMake(0, title.frame.size.height, self.scrollView.frame.size.width, rect.size.height + 20)];
        des.text = str;
        des.font = [UIFont systemFontOfSize:14];
        des.numberOfLines = 0;
        des.textColor = [UIColor whiteColor];
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width *(i - 1), self.scrollView.frame.size.height / 3 * 2 + 10, self.scrollView.frame.size.width, title.frame.size.height + des.frame.size.height)];
        backView.tag = 100 + i;
        backView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0];
        [backView addSubview:title];
        [backView addSubview:des];
       
        
        _scrollView.showsHorizontalScrollIndicator = NO;
        scrollSmall.delegate = self;
        scrollSmall.minimumZoomScale = 0.5;
        scrollSmall.maximumZoomScale = 3;
        _scrollView.delegate = self;
        self.scrollView.pagingEnabled = YES;
        [scrollSmall addSubview:imageView];
        [self.scrollView addSubview:scrollSmall];
        [self.scrollView addSubview:backView];
        _scrollView.bounces = NO;
        
        [scrollSmall release];
        [imageView release];
        [backView release];
        [title release];
        [des release];
    
    }
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    UIImageView *image = (UIImageView *)tap.view;
    if (image.tag < 50) {
        UIView *view = (UIView *)[self.scrollView viewWithTag:(100 + image.tag)];
        [view setHidden:YES];
        self.navigationController.navigationBar.hidden = YES;
        image.tag += 50;
    } else {
        UIView *view = (UIView *)[self.scrollView viewWithTag:(100 + image.tag - 50)];
        [view setHidden:NO];
        self.navigationController.navigationBar.hidden = NO;
        image.tag -= 50;
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    return [scrollView.subviews objectAtIndex:0];
}


#pragma mark- scroll的协议

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x/scrollView.frame.size.width;
    UILabel *titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    titleView.text = [NSString stringWithFormat:@"%lu / %lu",  (unsigned long)page + 1, (unsigned long)self.array.count];
    titleView.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleView;

    //图片变大后还原
    if (self.scrollView == scrollView) {
        for (UIScrollView *small in self.scrollView.subviews) {
            if ([small isKindOfClass:[UIScrollView class]]) {
                [small setZoomScale:1.0f];
            }
        }
    }
}


// 返回按钮
- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
